// pages/new/new.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ["自己", "父母", "子女", "爱人", "其他"],
    index: "",
    items: [{
        name: '0',
        value: '男'
      },
      {
        name: '1',
        value: '女'
      },
    ],
    date: "",
    archivesArr: [
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //当前日期
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      bgc: bgc
    })
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) :
      nowDate.getMonth() + 1;
    var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
      .getDate();
    var dateStr = year + "-" + month + "-" + day;
    console.log(dateStr)
    that.setData({
      dateStr: dateStr
    })
    //获取是否关系里面有自己
    var archivesArr = that.data.archivesArr
    for (var i = 0, len = archivesArr.length; i < len; i++) {
      if (archivesArr[i].relationship == "自己") {
        //如果有自己 关系里面就不会再次出现自己
        var array = that.data.array
        array.splice(0, 1)
        that.setData({
          array: array
        })
      }
    }
  },
  bindPickerChange(e) {
    var array = this.data.array
    var index = e.detail.value
    this.setData({
      index: e.detail.value,
      guanx:array[index]
    })
    console.log()
  },
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  //提交
  subClick: function(e) {
    console.log(e)
    var that = this
    var names = e.detail.value.userName
    var relationship = that.data.guanx
    var sex = e.detail.value.sex
    var datetime = e.detail.value.date
    var age = e.detail.value.age
    if (e.detail.value.date == "") {
      wx.showToast({
        title: '请选择出生日期',
        icon: "none"
      })
    } else if (e.detail.value.relationship == "") {
      wx.showToast({
        title: '请选择您们的关系',
        icon: "none"
      })
    } else if (e.detail.value.sex == "") {
      wx.showToast({
        title: '请选择您的性别',
        icon: "none"
      })
    } else if (e.detail.value.userName == "") {
      wx.showToast({
        title: '请输入您的姓名',
        icon: "none"
      })
    } else {
      //全部完成回退
      app.util.request({
        'url': 'entry/wxapp/Addtijian',
        data: {
          openid: wx.getStorageSync('openid'),
          names: names,
          relationship: relationship,
          sex: sex,
          datetime: datetime,
          age: age
        },
        success: function(res) {
          wx.showToast({
            title: '提交成功',
            icon: 'none',
            success: function() {
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            }
          })
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})